// pages/myorder/myorder.js
var app = getApp()
import {
  postUserOrder, postOrderQx
} from '../../api/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl: app.globalData.baseUrl,
    listTitle: [
      "全部订单", "待付款", "已付款"
    ],
    listDingdan: [
      // {
      //   id: 1, time: "2019-8-24", status: "待付款", money: "19.00", total: "3", vipmoney: "15.00", quxiao: "取消订单", state: "1", productlist: [
      //     { image: "https://www.youdaosx.com/webImage/images/index/19.png"},
      //     { image: "https://www.youdaosx.com/webImage/images/index/20.png"},
      //     { image: "https://www.youdaosx.com/webImage/images/index/21.png"},
      //     { image: "https://www.youdaosx.com/webImage/images/index/22.png"},
      // ]}
    ],
    indexId: 0,
    state:''
  },
  //头部tab切换
  jumpIndex(e) {
    let index = e.currentTarget.dataset.menuindex
    // console.error(index)
    let that = this
    this.setData({
      indexId: index
    })
    this.getOrderData()
    // if (index == 0) {
    //   this.getOrderData()
    // } else {
    //   this.getOrderData(index)
    // }
  
  },
  //去支付
  onPayJump(e) {
    // wx.showLoading()
    wx.navigateTo({
      url: '../okOrder/okOrder?id=' + e.currentTarget.dataset.id,
    })
  },
  //取消订单
  onCancel(e){
    var that=this
    wx.showModal({
      title: '提示',
      content: '是否要删除订单?',
      success(res) {
        if (res.confirm) {
          postOrderQx({id:e.currentTarget.dataset.id}).then(res => {
              wx.showToast({
                title: '删除成功',
                icon:'success',
                duration:2000
              })
          })
          that.getOrderData()
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      indexId:options.state
    })
    this.data.state = options.state
  },
  //获取数据
  getOrderData(state) {
    var state=this.data.indexId
    if(this.data.indexId==0){
      state=''
    }
      postUserOrder({
        state: state
      }).then(res => {
        if (res.state == 0) {
          wx.hideLoading()
          this.setData({
            listDingdan: res.data
          })
          console.log(res.data)
        }
      })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
      // wx.showLoading({
      //   title: '加载中',
      // })
      this.getOrderData()
      // this.setData({
      //   indexId: 0
      // })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})